<?php
namespace Mentors\Controller;

class IndexController extends MainController {

    public function index(){

        if (!$this->baseInfo) {
            $this->redirect(U("Profile/index"));
        }     
           
        $userID=$this->userID;
        $userInfo['mentees']=M("Apprentice")->where("toID=".$userID." and status=0")->count();
        $userInfo['project']=M("Train")->where("mentorsID=".$userID." and status=0")->count();
        $userInfo['appointment']=M("Consult")->where("toID=".$userID." and status=0")->count();
        $userInfo['message']=M("Message")->where("toID=".$userID." and isread=0 and type=1 and toDel=0 ")->count();
        $this->assign("userInfo",$userInfo);

        //系统消息
        $sysmsg=M("Message")->where("toID=".$userID." and type=0 and isread=0")->order("id desc")->select();
        $this->assign("sysmsg",$sysmsg);

        //及时聊天信息
        $chat=M("Chat")->where("toID=".$userID."")->order("id desc")->limit("0,5")->select();
        $this->assign("chat",$chat);

        //今天需要做的事（导师预约与学习计划）
        $todo=array();
        $time1=strtotime(date('Y-m-d'));
        $time2=strtotime(date('Y-m-d',strtotime('+1 day')));
        $time="time>".($time1-1)." and time<".($time2+1);
        $i=0;
        $yuyue=M("Consult")->where("(toID=".$userID." or fromID=".$userID.") and status=1 and ".$time)->order("id desc")->select();
        foreach ($yuyue as $key => $value) {
            $todo[$i]['type']="appointment";
            if($value['toID'==$userID]){
               $todo[$i]['title']="【Appointment】Form:<b>".$value['fromName']."</b>"; 
            }else{
               $todo[$i]['title']="【Appointment】To:<b>".$value['fromName']."</b>"; 
            }
            $todo[$i]['time']=$value['createTime'];  
            $todo[$i]['link']=U('Appointment/edit',array('id'=>$value['id']));
            $i++;
        }
        unset($time);
        $time="startTime<".($time2+1)." and endTime>".($time2-1);
        $plan=M("PlanDetails")->where("mentorsID=".$userID." and status=0 and ".$time)->order("id desc")->select();
        foreach ($plan as $key => $value) {
            $todo[$i]['type']="plan";
            $todo[$i]['title']="【Plan】".$value['title'].""; 
            $trainID=M("Plan")->where("id=".$value['planID']."")->getField("trainID");
            $todo[$i]['link']=U('Plan/node',array('planID'=>$value['planID'],'trainID'=>$trainID)); 
            $i++;
        }
        $this->assign("todo",$todo);

    	$this->display();
    }

    //获取email的及时信息
    public function getEmailMsg(){
    	$map['toID']=$this->userID;
    	$map['isread']=0;
    	$map['toDel']=0;
    	$map['type']=1;
    	$list=M("Message")->where($map)->order("id desc")->select();
    	foreach ($list as $key => $value) {
    		$list[$key]['fromFace']=getFace($value['fromFace']);
    		$list[$key]['createTime']=mdate($value['createTime']);
    	}
    	$ret=array(
    		"status"=>"SUCCESS",
    		"total"=>count($list),
    		"data"=>$list
    		);
    	$this->ajaxReturn($ret);
    }

    //获取系统通知
    public function getSysMsg(){
        $map['toID']=$this->userID;
        $map['isread']=0;
        $map['toDel']=0;
        $map['type']=0;
        $list=M("Message")->where($map)->order("id desc")->select();
        foreach ($list as $key => $value) {
            $list[$key]['createTime']=mdate($value['createTime']);
        }
        $ret=array(
            "status"=>"SUCCESS",
            "total"=>count($list),
            "data"=>$list
            );
        $this->ajaxReturn($ret);
    }

     //获取及时聊天信息
    public function getChatMsgLoop(){
    	$endID=I("endID",0);
    	$map['toID']=$this->userID;
    	$map['isread']=0;
    	$map['id']=array("GT",$endID);
    	$list=M("Chat")->where($map)->order("id desc")->find();
    	if($list){
    		$list['fromFace']=getFace($value['fromFace']);
	    	$list['createTime']=mdate($value['createTime']);
	    	$ret=array(
	    		"status"=>"SUCCESS",
	    		"data"=>$list
	    		);
    	}else{
    		$ret=array(
	    		"status"=>"ERROR"
	    		);
    	}
    	$this->ajaxReturn($ret);
    }
}